package com.winit.openapi.wh.validator;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.vo.InboundEnumConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单合并校验
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * bing.gu 	1.0  		2016年1月26日 	Created
 *
 * </pre>
 * @since 1.
 */
public class InboundOrderMergeValidator {

    /**
     * 分页查询模式
     */
    private static final String QUERY_MODE_PAGE = "P";
    /**
     * 列表查询模式
     */
    private static final String QUERY_MODE_LIST = "L";

    /**
     * 可合并订单参数查询校验
     * 
     * @param json
     */
    public static void validCanMergeQueryParam(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderType", 30, false);
        if (json.containsKey("mainNo")) {
            ValidateUtil.validMaxLength(json, "mainNo", 64, true);
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "mainNo");
        }
        if (json.containsKey("subsidiaryNo") && StringUtils.isNotBlank(json.getString("subsidiaryNo"))) {
            ValidateUtil.validMaxLength(json, "subsidiaryNo", 64, false);
        } else {
            validQueryMode(json);
        }
    }

    /**
     * 合并订单参数校验
     * 
     * @param json
     */
    public static void validMergeParam(JSONObject json) {
        if (json.containsKey("mainNo")) {
            ValidateUtil.validMaxLength(json, "mainNo", 64, true);
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "mainNo");
        }
        JSONArray subOrderNoList = json.getJSONArray("subsidiaryNo");
        if (null == subOrderNoList || subOrderNoList.size() < 1) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "subsidiaryNo");
        }
        for (Object object : subOrderNoList) {
            JSONObject orderNoObj = (JSONObject) object;
            ValidateUtil.validMaxLength(orderNoObj, "orderNo", 64, true);
        }
    }

    /**
     * 根据主单查询子单校验
     * 
     * @param json
     */
    public static void validQuerySubOrderByMainOrderParam(JSONObject json) {
        if (json.containsKey("mainNo")) {
            ValidateUtil.validMaxLength(json, "mainNo", 64, true);
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "mainNo");
        }
        validQueryMode(json);
    }

    /**
     * 取消合并校验
     * 取消类型为按照主单取消(M)时，主单号必填
     * 取消类型为按照子单取消(L)时，子单号必填
     * @param json
     */
    public static void validMergeCancelParam(JSONObject json) {
        if (!json.containsKey("cancelType")) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "cancelType");
        }
        ValidateUtil.validMaxLength(json, "cancelType", 1, true);
        if (!InboundEnumConstant.OrderMergeType.MAIN.getCode().equals(json.getString("cancelType"))
            && !InboundEnumConstant.OrderMergeType.SUB.getCode().equals(json.getString("cancelType"))) {
            throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "cancelType");
        }
        if (InboundEnumConstant.OrderMergeType.MAIN.getCode().equals(json.getString("cancelType"))) {
            if (!json.containsKey("mainNo")) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "mainNo");
            }
            ValidateUtil.validMaxLength(json, "mainNo", 64, true);
        } else {
            JSONArray jsonArray = json.getJSONArray("subOrderNoList");
            if (null == jsonArray || jsonArray.size() < 1) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "subOrderNoList");
            }
            for (Object object : jsonArray) {
                JSONObject orderNoJson = (JSONObject) object;
                if (!orderNoJson.containsKey("surOrderNo")) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "surOrderNo");
                }
                ValidateUtil.validMaxLength(json, "surOrderNo", 64, true);
            }
        }
    }

    /**
     * 校验查询模式
     * 
     * @param json
     */
    private static void validQueryMode(JSONObject json) {
        if (json.containsKey("queryMode") && StringUtils.isNotBlank(json.getString("queryMode"))) {
            if (!QUERY_MODE_PAGE.equals(json.getString("queryMode"))
                && !QUERY_MODE_LIST.equals(json.getString("queryMode"))) {
                throw new ApiException(ErrorCode.FEILD_NOT_MATCH, "queryMode");
            }
            if (QUERY_MODE_PAGE.equals(json.getString("queryMode"))) {
                validPageParams(json.getJSONObject("pageParams"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "queryMode");
        }
    }

    /**
     * 校验分页参数
     * 
     * @param json
     */
    private static void validPageParams(JSONObject json) {
        if (null == json) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "pageParams");
        } else {
            if (!json.containsKey("pageSize")) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "pageSize");
            } else {
                ValidateUtil.validInteger(json, "pageSize", true);
            }
            if (!json.containsKey("pageNo")) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "pageNo");
            } else {
                ValidateUtil.validInteger(json, "pageNo", true);
            }
        }
    }
}
